package address

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"userop_bff/controller"
	"userop_bff/forms"
	"userop_bff/global"
	"userop_bff/model"
	"userop_bff/userop_proto"
)

// 地址列表
func List(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.Query("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	list, err := global.AddressSrvClient.GetAddressList(context.Background(), &userop_proto.AddressRequest{UserId: int32(userIdInt)})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	var addressList []model.Address
	for _, v := range list.Data {
		addressList = append(addressList, model.Address{
			Id:           int(v.Id),
			UserId:       v.UserId,
			Province:     v.Province,
			City:         v.City,
			District:     v.District,
			Address:      v.Address,
			SignerName:   v.SignerName,
			SignerMobile: v.SignerMobile,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "地址列表获取成功",
		"data": map[string]interface{}{
			"total":       list.Total,
			"addressList": addressList,
		},
	})
}

// 添加地址
func Add(c *gin.Context) {
	addressFrom := forms.AddressForm{}
	if err := c.ShouldBind(&addressFrom); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	address, err := global.AddressSrvClient.CreateAddress(context.Background(), &userop_proto.AddressRequest{
		UserId:       int32(userIdInt),
		Province:     addressFrom.Province,
		City:         addressFrom.City,
		District:     addressFrom.District,
		Address:      addressFrom.Address,
		SignerName:   addressFrom.SignerName,
		SignerMobile: addressFrom.SignerMobile,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "添加地址成功",
		"data": address.Id,
	})
}

// 删除地址
func Delete(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	addressId := c.PostForm("address_id")
	addressIdInt, _ := strconv.Atoi(addressId)
	_, err := global.AddressSrvClient.DeleteAddress(context.Background(), &userop_proto.AddressRequest{Id: int32(addressIdInt), UserId: int32(userIdInt)})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除地址成功",
		"data": nil,
	})
}

// 修改地址
func Update(c *gin.Context) {
	//userId, _ := c.Get("userId")
	//userIdInt := userId.(int32)
	userId := c.PostForm("user_id")
	userIdInt, _ := strconv.Atoi(userId)
	addressId := c.PostForm("address_id")
	addressIdInt, _ := strconv.Atoi(addressId)
	addressFrom := forms.AddressForm{}
	if err := c.ShouldBind(&addressFrom); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	_, err := global.AddressSrvClient.UpdateAddress(context.Background(), &userop_proto.AddressRequest{
		Id:           int32(addressIdInt),
		UserId:       int32(userIdInt),
		Province:     addressFrom.Province,
		City:         addressFrom.City,
		District:     addressFrom.District,
		Address:      addressFrom.Address,
		SignerName:   addressFrom.SignerName,
		SignerMobile: addressFrom.SignerMobile,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "修改地址成功",
		"data": nil,
	})
}
